// *************************************************************************************************
//
// MK3D Engine
// --------------------------------------
// Copyright (C) 2007-2011 Zeb (mailto: zebbey@gmail.com)
//
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
//
// *************************************************************************************************

#include "mk_header.h"
#include "mk_particle_modifier_size.h"

namespace MK3DE
{
	void CParticleModifierSize::Apply(Particle* particle, DWORD dt)
	{
		GetValue(particle->Life, particle->Size);
	}

	BOOL CParticleModifierSize::GetValue(DWORD dwLife, D3DXVECTOR2& data)
	{
		if (_vecKeyFrame.size() == 0)
			return FALSE;

		std::vector<__KeyFrame>::iterator i = _vecKeyFrame.begin();
		while (i != _vecKeyFrame.end())
		{
			__KeyFrame& keyFrame = *i;
			if (keyFrame.Life < dwLife)
			{
				if (i == _vecKeyFrame.begin())
				{
					data = keyFrame.Size;
					return TRUE;
				}
				else
				{
					__KeyFrame& keyFrame2 = *(i - 1);
					float s = (float)(keyFrame2.Life - dwLife) / (keyFrame2.Life - keyFrame.Life);
					D3DXVec2Lerp(&data, &keyFrame2.Size, &keyFrame.Size, s);

					return TRUE;
				}
			}

			++i;
		}

		__KeyFrame& keyFrame = *(i - 1);
		data = keyFrame.Size;
		return TRUE;
	}

	BOOL CParticleModifierSize::SetKeyFrame(DWORD dwLife, D3DXVECTOR2& data)
	{
		std::vector<__KeyFrame>::iterator i = _vecKeyFrame.begin();
		while (i != _vecKeyFrame.end())
		{
			__KeyFrame& keyFrame = *i;
			if (dwLife == keyFrame.Life)
			{
				keyFrame.Life = dwLife;
				keyFrame.Size = data;

				return TRUE;
			}
			else if (dwLife > keyFrame.Life)
				break;

			++i;
		}

		__KeyFrame keyFrame;
		keyFrame.Life = dwLife;
		keyFrame.Size = data;

		_vecKeyFrame.insert(i, keyFrame);
		return TRUE;
	}

	BOOL CParticleModifierSize::GetKeyFrame(DWORD dwLife, /* out */ D3DXVECTOR2& data)
	{
		std::vector<__KeyFrame>::iterator i = _vecKeyFrame.begin();
		while (i != _vecKeyFrame.end())
		{
			__KeyFrame& keyFrame = *i;
			if (dwLife == keyFrame.Life)
			{
				data = keyFrame.Size;
				return TRUE;
			}

			++i;
		}

		return FALSE;
	}

	BOOL CParticleModifierSize::GetKeyFrameByIndex(DWORD index, /* out */ DWORD& dwLife, /* out */ D3DXVECTOR2& data)
	{
		if (index >= _vecKeyFrame.size())
			return FALSE;

		__KeyFrame& keyFrame = *(_vecKeyFrame.begin() + index);
		dwLife = keyFrame.Life;
		data = keyFrame.Size;
		return TRUE;
	}

	BOOL CParticleModifierSize::RemoveKeyFrame(DWORD index)
	{
		if (index >= _vecKeyFrame.size())
			return FALSE;

		_vecKeyFrame.erase(_vecKeyFrame.begin() + index);
		return TRUE;
	}
}